// pages/pay/receiveVouchers/receiveVouchers.js
const app = getApp()
const WXAPI = require("../../../api/api")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    merchantId: {
      type: String,
      value: {},
    },
 
  },

  /**
   * 组件的初始数据
   */
  data: {
    tagList: [],
    pageNum: 1,
    pageSize: 10,
    vouchersList: [],
    isPull: true,//是否刷新
    redemptionHttp: app.globalData.redemptionHttp,
  },
  attached(){
    this.getTag()
    this.couponList()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleChos(){
      this.triggerEvent('receiveChose')
    },
    //获取优惠信息标签
    getTag(){
      var data = {
        merchantId: this.data.merchantId,
      }
      WXAPI.discountTag(data, (res) => {
        this.setData({
          tagList: res.result,//拥有的卡
        })
      })
    },
    //获取优惠券列表
    couponList(){
      var data = {
        merchantId: this.data.merchantId,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        // merchantId: '1428651644106575874'
      }
      WXAPI.couponList(data, (res) => {
        const {size,total,records} = res.result;
        var list = [];
        var vouchersList = this.data.vouchersList;
        if (res.code === 200) {
          if (this.data.pageNum >= Math.ceil(total / size)) {  
            this.setData({
              lodingtype: 1,
              isPull: false
            });
          } else {
            this.setData({
              lodingtype: 0,
              isPull: true
            });
          }

          wx.hideLoading();
          list = records, this.setData({
            vouchersList: vouchersList.concat(list)
          });
        }
      })
    },
    //跳转店铺
    handleShop(e){
      var item = e.currentTarget.dataset.item
      if(item.type == 1){
        wx.navigateTo({
          url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId='+item.shopId,
        })
      }else if(item.type == 2){
        wx.navigateTo({
          url: '/userPageC/pages/shopBulk/shopBulk?shopId='+item.shopId,
        })
      }
    },
    //立即领取
    handleReceive(e){
      var id = e.currentTarget.dataset.id
      //可用门店
      var url = this.data.redemptionHttp + 'pages/vouchersDetail/vouchersDetail'
      var obj = {
        id: id,
        phone:  wx.getStorageSync('phone'),
        time: new Date().getTime()
      }
      obj = JSON.stringify(obj)
      wx.navigateTo({
        url: '/userPageC/pages/externalH5/externalH5?url='+url+'&data='+obj+'&type=true',
      })
    },
    //滚到底部时触发
    lower(){
      console.log('1111111111')
      if (this.data.isPull == true) {
        var pageNum = this.data.pageNum;
        pageNum++;
        console.log("触底了", pageNum);
        this.setData({
          pageNum: pageNum
        });
        this.couponList();
      }
    },
  }
})
